package com.alogic.swagger.redoc;

import com.alogic.xscript.AbstractLogiclet;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import org.apache.commons.lang3.StringUtils;

/**
 * redoc扩展LOGO
 *
 * @since 1.6.13.7 [20200812 duanyy] <br>
 */
public class SwaggerLogo extends AbstractLogiclet {
    protected String $bgColor;
    protected String $url;
    protected String $altText;
    protected String $href;

    public SwaggerLogo(String tag, Logiclet p) {
        super(tag, p);
    }

    @Override
    public void configure(Properties p){
        super.configure(p);

        $bgColor = PropertiesConstants.getRaw(p,"bgColor","");
        $altText = PropertiesConstants.getRaw(p,"text","");
        $url = PropertiesConstants.getRaw(p,"url","");
        $href = PropertiesConstants.getRaw(p,"href","");
    }

    @Override
    protected void onExecute(XsObject root, XsObject current, final LogicletContext ctx, final ExecuteWatcher watcher){
        XsObject newCurrent = current.getObjectChild("x-logo",true);
        newCurrent.addProperty("url",PropertiesConstants.transform(ctx,$url,""));
        newCurrent.addProperty("altText",PropertiesConstants.transform(ctx,$altText,""));
        newCurrent.addProperty("href",PropertiesConstants.transform(ctx,$href,""));
        if (StringUtils.isNotEmpty(PropertiesConstants.transform(ctx,$bgColor,""))) {
            newCurrent.addProperty("backgroundColor", PropertiesConstants.transform(ctx, $bgColor, "#FFFFFF"));
        }
    }
}
